import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import oracle.jdbc.pool.OracleDataSource;

import org.junit.Test;

import com.googlecode.openauth.client.UserPrivilege;
import com.googlecode.openauth.entity.ActionInfo;
import com.googlecode.openauth.entity.AreaInfo;
import com.googlecode.openauth.entity.MenuInfo;
import com.googlecode.openauth.entity.ResourceInfo;
import com.googlecode.openauth.exception.ValidateException;

import junit.framework.TestCase;


public class TestUserPrivilege extends TestCase {
	
	@Test
	public void testUserPrivilegeByDataSource() {
		try {
			OracleDataSource dataSource = new OracleDataSource();
			dataSource.setDriverType("oracle.jdbc.driver.OracleDriver");
			dataSource.setURL("jdbc:oracle:thin:@127.0.0.1:1521:orcl");
			dataSource.setUser("fox");
			dataSource.setPassword("123456");
			
			UserPrivilege pri = new UserPrivilege(dataSource);
			boolean res = pri.validate("admin", "123456");
			System.out.println(res);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ValidateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	public void testUserPrivilegeByConfig() {
		UserPrivilege pri = new UserPrivilege("ORACLE",
				"jdbc:oracle:thin:@127.0.0.1:1521:orcl", "fox", "123456");
		try {
			boolean res = pri.validate("admin", "123456");
		} catch (ValidateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	public void testUserPrivilege() {
		try {
			UserPrivilege pri = new UserPrivilege();
			boolean loginRes = pri.validate("admin", "23456");
			if (loginRes) {
				List<MenuInfo> menuList = pri.getMenus();
				for (MenuInfo menu : menuList) {
					System.out.println("menu:" + menu.getMenuId() + " " + menu.getMenuName());
				}
				boolean res = pri.validateUserMenuName("testMenu");
				res = pri.validateUserMenuId(1);
				if (res) {
					System.out.println("true");
				} else {
					System.out.println("false");
				}
			}
			List<ActionInfo> actionlist = pri.getActions();
			List<AreaInfo> areaList = pri.getAreas();
			List<MenuInfo> menuList = pri.getMenus();
			List<ResourceInfo> resList = pri.getResources();
			areaList = pri.getChildrenAreas("");
			menuList = pri.getChildrenMenus("");
			areaList = pri.getOneLevelAreas();
			menuList = pri.getOneLevelMenus();
			pri.validateUserActionId(1);
			pri.validateUserActionName("");
			pri.validateUserActionUuid("");
			pri.validateUserAreaId(1);
			pri.validateUserAreaName("");
			pri.validateUserAreaUuid("");
			pri.validateUserMenuId(1);
			pri.validateUserMenuName("");
			pri.validateUserMenuUuid("");
			pri.validateUserResId(1);
			pri.validateUserResName("");
			pri.validateUserResUuid("");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ValidateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
